//
//  BaseView.m
//  MyMicroBlog
//
//  Created by Apple on 16/9/21.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "BaseView.h"
#import "Masonry.h"
@interface BaseView()
//风火轮视图
@property(strong,nonatomic)UIImageView *cycleImageV;
//罩层
@property(strong,nonatomic)UIImageView *maskImageV;
//主页
@property(strong,nonatomic)UIImageView *iconImageV;
//主页信息
@property(strong,nonatomic)UILabel *messageLabel;
//登录按钮
@property(strong,nonatomic)UIButton *loginBtn;
//注册按钮
@property(strong,nonatomic)UIButton *resignBtn;

@end

@implementation BaseView

//修改访客视图
-(void)updateVisitorViewWithMessage:(NSString *)message andIconImage:(NSString *)iconImage{
    if (message && iconImage) {
        //信息非空,其他页面
        _messageLabel.text = message;
        _iconImageV.image = [UIImage imageNamed:iconImage];
        //隐藏风火轮
        _cycleImageV.hidden = YES;
    }else{
        //信息为空,默认为首页,执行动画
        [self startAnimation];
    }
}

//设置动画
-(void)startAnimation{
    CABasicAnimation *animation = [CABasicAnimation animation];
    animation.keyPath = @"transform.rotation.z";
    animation.toValue = [NSNumber numberWithFloat:2 * M_PI];
    animation.duration = 10;
    animation.repeatCount = CGFLOAT_MAX;
    //失去焦点不释放动画
    animation.removedOnCompletion = NO;
    [_cycleImageV.layer addAnimation:animation forKey:nil];
}

//懒加载控件
-(UIImageView *)cycleImageV{
    _cycleImageV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_smallicon"]];
    return _cycleImageV;
}

-(UIView *)maskImageV{
    _maskImageV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"]];
    return _maskImageV;
}

-(UIImageView *)iconImageV{
    _iconImageV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_house"]];
    return _iconImageV;
}

-(UILabel *)messageLabel{
    _messageLabel = [[UILabel alloc]init];
    _messageLabel.text = @"关注一些人,回这里看看有什么惊喜关注一些人,回这里看看有什么惊喜";
    _messageLabel.font = [UIFont systemFontOfSize:14];
    _messageLabel.numberOfLines = 0;
    _messageLabel.textAlignment = NSTextAlignmentCenter;
    _messageLabel.textColor = [UIColor darkGrayColor];
    return _messageLabel;
}

-(UIButton *)loginBtn{
    _loginBtn = [[UIButton alloc]init];
    [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [_loginBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [_loginBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
    [_loginBtn setBackgroundImage:[UIImage imageNamed:@"common_button_white_disable"] forState:UIControlStateNormal];
    _loginBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    _loginBtn.adjustsImageWhenHighlighted = NO;
    [_loginBtn addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];
    return _loginBtn;
}

-(UIButton *)resignBtn{
    _resignBtn = [[UIButton alloc]init];
    [_resignBtn setTitle:@"注册" forState:UIControlStateNormal];
    [_resignBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [_resignBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
    [_resignBtn setBackgroundImage:[UIImage imageNamed:@"common_button_white_disable"] forState:UIControlStateNormal];
    _resignBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    _resignBtn.adjustsImageWhenHighlighted = NO;
    [_resignBtn addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];
    return _resignBtn;
}

//设置约束
-(void)layoutSubviews{
    [super layoutSubviews];
    [_cycleImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
    }];
    
    [_maskImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
    }];
    
    [_iconImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
    }];
    
    [_messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_cycleImageV);
        make.top.equalTo(_cycleImageV.mas_bottom);
        make.width.mas_equalTo(224);
    }];
    
    [_loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(_messageLabel.mas_leading);
        make.top.equalTo(_messageLabel.mas_bottom).offset(10);
        make.size.mas_equalTo(CGSizeMake(100, 35));
    }];
    
    [_resignBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(_messageLabel.mas_trailing);
        make.top.equalTo(_loginBtn);
        make.size.equalTo(_loginBtn);
    }];
}

//点击事件
-(void)loginAction{
    self.loginBlock();
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupUI];
    }
    return self;
}

#pragma mark -搭建界面
-(void)setupUI{
    self.backgroundColor = [UIColor colorWithWhite:237  / 255.0 alpha:1.0];
    //添加子控件
    [self addSubview:self.cycleImageV];
    [self addSubview:self.maskImageV];
    [self addSubview:self.iconImageV];
    [self addSubview:self.messageLabel];
    [self addSubview:self.loginBtn];
    [self addSubview:self.resignBtn];
    //使用系统自动布局
    _cycleImageV.translatesAutoresizingMaskIntoConstraints = NO;
}

@end
